/*
 * Copyright (c) 2001-2023 cnstrong.cn Corporation Limited. All rights reserved.
 * This software is the confidential and proprietary information of cnstrong.cn Company.
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with cnstrong.cn.
 */
package com.treeconfig.render.service.impl;

import com.treeconfig.render.dataobject.ProjectUserDO;
import com.treeconfig.render.exception.ProjectUserCheckException;
import com.treeconfig.render.mapper.MicrodomainProjectUserMapper;
import com.treeconfig.render.param.ListProjectUserParam;
import com.treeconfig.render.param.SaveProjectUserParam;
import com.treeconfig.render.po.ListProjectUserPO;
import com.treeconfig.render.po.ProjectUserPO;
import com.treeconfig.render.service.ProjectUserService;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

/**
 * ProjectUserServiceImpl
 *
 * @author Braveheart
 * @version V1.0
 * @since 2023-12-29
 */
public class ProjectUserServiceImpl implements ProjectUserService {

    @Resource
    private MicrodomainProjectUserMapper microdomainProjectUserMapper;

    @Override
    public String getDefaultProjectCodeByOa(String oa) {
        return microdomainProjectUserMapper.selectDefaultProjectByOa(oa);
    }

    @Override
    public int saveProjectUser(SaveProjectUserParam param) {
        if (Objects.isNull(param.getId())) {
            return add(param);
        }
        return update(param);
    }

    @Override
    public Page<ProjectUserDO> listProjectUser(ListProjectUserParam param) {
        ListProjectUserPO po = new ListProjectUserPO();
        po.setPageNumber(param.getPageNumber());
        po.setPageSize(param.getPageSize());
        po.setProjectCode(param.getProjectCode());
        po.setUserOa(param.getUserOa());
        int count = microdomainProjectUserMapper.listProjectUserCount(po);
        if (count <= 0) {
            return Pages.page(param, null, 0);
        }
        List<ProjectUserDO> result = microdomainProjectUserMapper.listProjectUser(po);
        return Pages.page(param, result, count);
    }

    @Override
    public int deleteById(Long id, String operateOa) {
        return microdomainProjectUserMapper.deleteById(id);
    }

    @Override
    public ProjectUserDO getById(Long id) {
        return microdomainProjectUserMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int setDefaultProject(Long id, String operateOa) {
        Long defaultProjectId = microdomainProjectUserMapper.selectDefaultProjectIdByOa(operateOa);
        int setResult = microdomainProjectUserMapper.setDefaultProject(id);
        if (setResult == 1 && Objects.nonNull(defaultProjectId)) {
            int cancelResult = microdomainProjectUserMapper.cancelDefaultProject(defaultProjectId);
            if (cancelResult != 1) {
                throw new ProjectUserException("取消原默认项目失败");
            }
        }
        return 1;
    }

    private int add(SaveProjectUserParam param) {
        ProjectUserPO checkPo = new ProjectUserPO();
        checkPo.setProjectCode(param.getProjectCode());
        checkPo.setUserOa(param.getUserOa());
        if (Objects.nonNull(microdomainProjectUserMapper.selectByUserAndCode(checkPo))) {
            throw new ProjectUserCheckException("成员重复");
        }
        ProjectUserDO projectUserDO = new ProjectUserDO();
        projectUserDO.setProjectCode(param.getProjectCode());
        projectUserDO.setUserName(param.getUserName());
        projectUserDO.setUserOa(param.getUserOa());
        projectUserDO.setRoleCode(param.getRoleCode());
        projectUserDO.setIsDefaultProject(DefaultProjectEnum.NO.getType());
        // 第一次被添加为项目成员默认设置成默认项目
        if (CollectionUtils.isEmpty(microdomainProjectUserMapper.selectByUserOa(param.getUserOa()))) {
            projectUserDO.setIsDefaultProject(DefaultProjectEnum.YES.getType());
        }
        projectUserDO.setIsDeleted(0);
        return microdomainProjectUserMapper.insert(projectUserDO);
    }

    private int update(SaveProjectUserParam param) {
        ProjectUserDO projectUserDO = new ProjectUserDO();
        projectUserDO.setId(param.getId());
        projectUserDO.setRoleCode(param.getRoleCode());
        return microdomainProjectUserMapper.updateByPrimaryKeySelective(projectUserDO);
    }
}
